
<#include "/base/common/head.html"/>
    
    <table id="dg"></table>
    <div id="tb" style="padding:2px 5px;">
        <div class="wrap_search">
				            
			            	<span class="search_item">
				            	<span class="item_text">用车部门：</span>
				            	<span class="item_obj">
				            		<input class="easyui-combobox" type="text" name="organId" id="organId" value=""  addClear
               							data-options="valueField:'id',textField:'name',url:'${basePath}/car/outbill/useOrgan/findAll',editable:false"></input>
				            	</span>    
				            </span>
				            
			            	<span class="search_item">
				            	<span class="item_text">用车人：</span>
				            	<span class="item_obj">
				            		<input class="easyui-textbox" type="text" name="user" id="user" value="" addClear data-options=""></input>
				            	</span>    
				            </span>
				            
			            	<span class="search_item">
				            	<span class="item_text">车辆：</span>
				            	<span class="item_obj">
				            		<input class="easyui-combobox" type="text" name="carId" id="carId" value="" addClear 
               							data-options="valueField:'id',textField:'name',url:'${basePath}/car/car/car/findAll',editable:false"></input>
				            	</span>    
				            </span>
				            
			            	<span class="search_item">
				            	<span class="item_text">司机：</span>
				            	<span class="item_obj">
				            		<input class="easyui-combobox" type="text" name="driver" id="driver" value=""  addClear
               							data-options="valueField:'name',textField:'name',url:'${basePath}/base/basis/enumDetail/findByKey?key=driver'"></input>
				            	</span>    
				            </span>
				            
			            	<span class="search_item">
				            	<span class="item_text">车辆状况：</span>
				            	<span class="item_obj">
				            		<input class="easyui-combobox" type="text" name="carStatus" id="carStatus" value=""  addClear
               							data-options="valueField:'name',textField:'name',url:'${basePath}/base/basis/enumDetail/findByKey?key=carStatus'">
				            	</span>    
				            </span>
				            
		        			<span class="search_item search_between">
				                <span class="item_text">出发时间：</span>
				                <span class="item_obj">
				                    <input class="easyui-datebox" name="beginTime_start" id="beginTime_start" value="" data-options="editable:false,width:'110px'" addClear/> ~ <input class="easyui-datebox" name="beginTime_end" id="beginTime_end" value="" addClear data-options="editable:false,width:'110px'"/>
				                </span>
				            </span>
		        		
        </div>
        <div style="text-align: left; margin:6px;">
        	<!--  
            -->
            <a href="javascript:void(0)" class="easyui-linkbutton updateBtn" iconCls="glyphicon-pencil" plain="true" onclick="update()">回录</a>
            <a href="javascript:void(0)" class="easyui-linkbutton delBtn" iconCls="glyphicon-remove" plain="true" onclick="del()">删除</a>
            <a href="javascript:void(0)" class="easyui-linkbutton detailBtn" iconCls="glyphicon-map-marker" plain="true" onclick="detail()">详情</a>
            
            <div id="searchBtnWrap" style="float:right; padding-right:40px;">
                <a href="javascript:void(0)" class="easyui-linkbutton color1" iconCls="glyphicon-search" onclick="zcurdSearch()">搜索</a>
                <a href="javascript:void(0)" class="easyui-linkbutton color2" iconCls="glyphicon-repeat" onclick="flushPage()">重置</a>
            </div>
            
        </div>
    </div>
    
<style type="text/css">
	.subtotal { font-weight: bold; }/*合计单元格样式*/
</style>

<script type="text/javascript">

//数据表格初始化参数
var dgOptions = {
        rownumbers:true,
    	fit:true,
    	border:false,
    	url:'listData',
    	method:'post',
    	toolbar:'#tb',
    	pageSize: 20,
    	pagination:true,
    	//fitColumns:true,
    	striped:true,
    	multiSort:false, //是否允许多列排序
		sortName:'addTime', //初始排序字段
		sortOrder:'desc', //初始排序方式
    	singleSelect: false,
		ctrlSelect : true,
		showFooter : true,
        queryParams: zcurdGetParam(),
        onDblClickRow:function(index, row){viewData(row.id);},
        //onLoadSuccess:onLoadSuccess, //自动合计。暂不用页面合计，用后台进行合计
        columns: [[
		         {field:'id', checkbox:true}
	           	 ,{field:'addTime', title: '创建时间', width:110, align:'center',sortable:true}
	           	 ,{field:'car', title: '车辆', width:80, align:'center', formatter:formatType,sortable:true}
	           	 ,{field:'driver', title: '驾驶员', width:60, align:'center',sortable:true}
	           	 ,{field:'user', title: '用车人', width:60, align:'center',sortable:true}
	           	 ,{field:'approver', title: '审批人', width:60, align:'center',sortable:true}
	           	 ,{field:'organ', title: '用车部门', width:80, align:'center', formatter:formatType,sortable:true}
	           	 ,{field:'destination', title: '目的地', width:100, align:'center'}
	           	 ,{field:'beginTime', title: '出发时间', width:110, align:'center',sortable:true}
	           	 ,{field:'endTime', title: '归队时间', width:110, align:'center',sortable:true}
	           	 ,{field:'useTime', title: '总时长', width:60, align:'center',sortable:true}
	           	 ,{field:'waitTime', title: '等待时长', width:70, align:'center',sortable:true}
	           	 ,{field:'beginNum', title: '出发里程', width:60, align:'center'}
	           	 ,{field:'endNum', title: '归队里程', width:60, align:'center'}
	           	 ,{field:'useNum', title: '使用里程', width:70, align:'center',sortable:true}
	           	 ,{field:'fuelNum', title: '加油金额', width:70, align:'center',sortable:true}
	           	 ,{field:'otherFee', title: '过 / 停费', width:70, align:'center',sortable:true}
	           	 ,{field:'allFee', title: '总费用', width:60, align:'center',sortable:true}
	           	 ,{field:'carStatus', title: '车辆状况', width:80, align:'center',sortable:true}
	           	 ,{field:'remark', title: '备注', width:100, align:'center'}
	           	 ,{field:'state', title: '状态', width:60, align:'center', formatter:formatState}
	           	 
	           ]],
	      loadFilter: function(data) {
	        	   if(data.result && data.result == 'fail') {	//失败时，错误消息提示
	        		   showWarnMsg(data.msg);
	        		   return {};
	        	   }else {
	        		   return data;
	        	   }
	           }           
	};
	
//自动求和
function onLoadSuccess() {
    datagrid.datagrid('reloadFooter', [
	{//标题，方便查看
	   	useTime: '总时长',
	   	waitTime: '等待时长',
	   	useNum: '使用里程',
	   	fuelNum: '加油金额',
	   	otherFee: '过 / 停费',
	   	allFee: '总费用',
	   	car:{"name":""}, //主要防止格式化调用出错
	   	organ:{"name":""}
	},
    {//合计数
    	endTime: '<span class="subtotal">合计</span>',
    	useTime: '<span class="subtotal">' + compute("useTime") + '</span>',
    	waitTime: '<span class="subtotal">' + compute("waitTime") + '</span>',
    	useNum: '<span class="subtotal">' + compute("useNum") + '</span>',
    	fuelNum: '<span class="subtotal">' + compute("fuelNum") + '</span>',
    	otherFee: '<span class="subtotal">' + compute("otherFee") + '</span>',
    	allFee: '<span class="subtotal">' + compute("allFee") + '</span>',
    	car:{"name":""},
    	organ:{"name":""}
    }
    ]);
}

//指定列求和
function compute(colName) {
    var rows = datagrid.datagrid('getRows');
    var total = 0;
    var valueStr;
    for (var i = 0; i < rows.length; i++) {
    	
    	valueStr=rows[i][colName];
    	if(valueStr!=null)
        {
    		total += parseFloat(valueStr);
        }
    }
    return total;
}



var datagrid = $("#dg"); //数据表格

$(function() {
	datagrid.datagrid(dgOptions); //绘制数据表格
	autoAddClear(); //为各组件自动填加‘清除’功能（组件需要增加 addClear属性 ）
});

//查询
function zcurdSearch() {
	var param = zcurdGetParam();
	datagrid.datagrid("load", param)
}

//获取查询参数
function zcurdGetParam() {
	var param = {};
	$("#tb :input[name]").each(function(i, item) {
		if($(item).val()) {
			param[$(item).attr("name")] = $(item).val();	
		}
	});
	//console.log(param);
	return param;
}

var initPara = zcurdGetParam(); //初始化参数，主要目的是保存一些隐藏条件


//修改
function update() {
	var rowsSel = datagrid.datagrid("getSelections");
	if(rowsSel.length != 1) {
		showWarnMsg("请选择需要编辑的一条数据");
		return;
	}
	var id = rowsSel[0].id;
	top.window.subPage.loadCurrDatagrid = function() {
		datagrid.datagrid("reload");
	}
	top.openWindow("回录", "${basePath}/car/outbill/outBill/updatePage?id=" + id, {size: '820x490'});
}

//详情
function detail() {
	var rowsSel = datagrid.datagrid("getSelections");
	if(rowsSel.length != 1) {
		showWarnMsg("请选择需要查看的一条数据");
		return;
	}
	var id = rowsSel[0].id;
	viewData(id)
}

//双击打开查看
function viewData(id) {
	top.openWindow_view("详情", getCurrUrl("detailPage") + "?id=" + id, {size: '780x490'});
}

//删除
function del() {
	var ids = [];
	$.each(datagrid.datagrid("getSelections"), function(i, item) {
		ids.push(item.id);
	});
	if(ids.length < 1) {
		showWarnMsg("请选择需要删除的数据");
		return;
	}
	confirmMsg("确认删除?", function() {
		$.post(getCurrUrl("delete"), {ids: ids.join(",")}, function(data) {
			
			if("01" == data.code){//成功
				datagrid.datagrid("reload");
				showMsg(data.msg); //显示消息
			}else if("04" == data.code){//失败
				showErrorMsg(data.msg); //显示错误消息
			}
			
		});
	});
}

//导出（未实现）
function exportCsv(type) {
	var option = datagrid.datagrid("options");
	var param = option.queryParams;
	param.sortName = option.sortName;
	param.sortOrder = option.sortOrder;
	param.type = type;
	window.location.href = "${basePath}/car/outbill/outBill/exportExcel?" + $.param(param);
}

/**
 * 打开子页（右侧，暂未用上）
 */
var _openSubPageFlag = false;
function _openSubPage(pageUrl) {
	if(!_openSubPageFlag) {
		$("#subLayout").layout("expand", "east");
		setTimeout(function() {
			$("#subIframe").attr("src", pageUrl);
		}, 600);
		_openSubPageFlag = true;
	}else {
		$("#subIframe").attr("src", pageUrl);
	}
}

//扩展js
//格式化类型对象的输出
function formatType(value)
{
	if(value == null)
	{
		return "";
	}
	return value.name;
};

function formatResultType(value)
{
	if(value.code=='01')
	{
		return value.name;
	}
	else
	{
		return "<span style='color:#ff0000'>"+value.name+"</span>";
	}
};


function formatState(value)
{
	if(value == null)
	{
		return "";
	}
	if(value.code=='07')
	{
		return "<span style='color:#ff0000'>"+value.name+"</span>";
	}
	return value.name;
};


</script>

<#include "/base/common/dialogWindow.html"/>
<#include "/base/common/foot.html"/>
